
package uk.addie.mp3fixer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

final public class Util {
    private static final char[]                    BAD_CHAR = new char[] { '"', '?', '<', '>', '\\', '/', '|', '*', ':' };
    private static final Map<Character, Character> BAD_CHAR_MAP;
    static {
        BAD_CHAR_MAP = new HashMap<>();
        for (final char c : BAD_CHAR) {
            BAD_CHAR_MAP.put(c, '_');
        }
    }

    public static String clean(final String input) {
        final StringBuilder rval = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            final char c = input.charAt(i);
            if (c > 128 || BAD_CHAR_MAP.containsKey(c)) {
                rval.append('_');
            } else {
                rval.append(c);
            }
        }
        return rval.toString();
    }

    static String extension(final String base) {
        String extension = "";
        final int i = base.lastIndexOf('.');
        if (i > 0) {
            extension = base.substring(i + 1);
        }
        return extension;
    }

    public static void moveFile(final Path from, final Path to) {
        try {
            Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            MP3Fixer.LOG.log(Level.SEVERE, "Can't move file", e);
        }
    }

    private Util() {
    }
}
